// composables/useSafeAreaInsets.ts
import { ref } from 'vue'

export interface SafeAreaInsets {
  top: number
  right: number
  bottom: number
  left: number
}

export function useSafeAreaInsets() {
  const safeAreaInsets = ref<SafeAreaInsets | null>(null)

  // #ifdef MP-WEIXIN
  const windowInfo = uni.getWindowInfo() // 类型：GetWindowInfoResult
  safeAreaInsets.value = windowInfo.safeArea
    ? {
        top: windowInfo.safeArea.top,
        right: windowInfo.windowWidth - windowInfo.safeArea.right,
        bottom: windowInfo.windowHeight - windowInfo.safeArea.bottom,
        left: windowInfo.safeArea.left,
      }
    : null
  // #endif

  // #ifndef MP-WEIXIN
  const systemInfo = uni.getSystemInfoSync() // 类型：GetSystemInfoResult
  safeAreaInsets.value = systemInfo.safeAreaInsets || null
  // #endif

  return {
    safeAreaInsets,
  }
}
